home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.04 Apr 91 / NonStandard File / NonStandard File Unit < prev    next >
Encoding:
Text File  |  1991-02-08  |  13.2 KB  |  572 lines  |  [TEXT/PJMM]

  1. {****************************}
  2. {}
  3. {    Non-Standard File Unit}
  4. {        Created by Steve Sheets}
  5. {}
  6. {        Provides 4 different new user interfaces for}
  7. {        the Get & Put dialogs.}
  8. {}
  9. {****************************}
  10.  
  11.  
  12. unit NonStandardFile;
  13.  
  14. interface
  15.  
  16.     type
  17.         SFTypeListPtr = ^SFTypeList;
  18.  
  19.         StrArray = array[1..1] of Str255;
  20.         StrArrayPtr = ^StrArray;
  21.  
  22. {    Four new Put & Get File Routines}
  23.  
  24.     procedure NSPutFile (thePrompt, theOrigName: Str255;
  25.                                     var theGood: BOOLEAN;
  26.                                     var theRefNum: INTEGER;
  27.                                     var theFileName: Str255);
  28.  
  29.     procedure NSSelectPutFile (thePrompt, theSelectPrompt, theOrigName: Str255;
  30.                                     var theGood: BOOLEAN;
  31.                                     var theRefNum: INTEGER;
  32.                                     var theFileName: Str255;
  33.                                     var theFlag: BOOLEAN);
  34.  
  35.     procedure NSIconPutFile (theOrigName: Str255;
  36.                                     theDefaultNum: INTEGER;
  37.                                     theIcon1, theIcon2, theIcon3, theIcon4: Handle;
  38.                                     theName1, theName2, theName3, theName4: Str255;
  39.                                     thePrompt1, thePrompt2, thePrompt3, thePrompt4: Str255;
  40.                                     var theGood: BOOLEAN;
  41.                                     var theRefNum: INTEGER;
  42.                                     var theFileName: Str255;
  43.                                     var theNum: INTEGER);
  44.  
  45.     procedure NSGetFile (thePrompt: Str255;
  46.                                     theNumTypes: INTEGER;
  47.                                     theTypeListPtr: SFTypeListPtr;
  48.                                     theNameListPtr: StrArrayPtr;
  49.                                     var theGood: BOOLEAN;
  50.                                     var theRefNum: INTEGER;
  51.                                     var theFileName: Str255;
  52.                                     var theType: OSType);
  53.  
  54. {************************************}
  55.  
  56. {    External references to these procedures.}
  57. {    Do not call them outside this unit}
  58.  
  59.     function NSSelectPutDlg (item: INTEGER;
  60.                                     theDialog: DialogPtr): INTEGER;
  61.  
  62.     procedure NSIconItem (theWindow: WindowPtr;
  63.                                     itemNo: INTEGER);
  64.  
  65.     function NSIconPutDlg (item: INTEGER;
  66.                                     theDialog: DialogPtr): INTEGER;
  67.  
  68.     procedure NSPopUpItem (theWindow: WindowPtr;
  69.                                     itemNo: INTEGER);
  70.  
  71.     function NSGetFileFilter (paramBlock: ParmBlkPtr): BOOLEAN;
  72.  
  73.     function NSGetDlg (item: INTEGER;
  74.                                     theDialog: DialogPtr): INTEGER;
  75.  
  76. implementation
  77.  
  78.     const
  79.         kNSGetID = 500;
  80.         kNSPutID = 501;
  81.         kNSSelectPutID = 502;
  82.         kNSIconPutID = 503;
  83.  
  84.         kGetPopUpID = 254;
  85.         kGetPopUpItem = 11;
  86.         kGetPopUpPrompt = 12;
  87.  
  88.         kPutPrompt = 3;
  89.         kPutNoSelectSwitch = 9;
  90.         kPutSelectSwitch = 10;
  91.         kPutIcon1 = 9;
  92.  
  93.         kMaxIcons = 4;
  94.  
  95.     type
  96.         IconStrPtrArray = array[1..kMaxIcons] of StringPtr;
  97.  
  98.     var
  99.         gNSCurNum, gNSMaxNum: INTEGER;
  100.         gNSTypeListPtr: SFTypeListPtr;
  101.         gNSPromptListPtr: IconStrPtrArray;
  102.         gNSNameListPtr: IconStrPtrArray;
  103.         gNSFirst, gNSSelect: BOOLEAN;
  104.         gNSMenuHdl: MenuHandle;
  105.         gNSBoxs: array[1..kMaxIcons] of Rect;
  106.         gNSIcons: array[1..kMaxIcons] of Handle;
  107.         gNSPrompt, gNSAltPrompt: Str255;
  108.  
  109. {************************************}
  110.  
  111. {    NSPutFile routines}
  112.  
  113.     procedure NSPutFile (thePrompt, theOrigName: Str255;
  114.                                     var theGood: BOOLEAN;
  115.                                     var theRefNum: INTEGER;
  116.                                     var theFileName: Str255);
  117.         var
  118.             tempPt: Point;
  119.             tempReply: SFReply;
  120.     begin
  121.         tempPt.v := 40;
  122.         tempPt.h := 60;
  123.         theGood := FALSE;
  124.  
  125.         SFPPutFile(tempPt, thePrompt, theOrigName, nil, tempReply, kNSPutID, nil);
  126.  
  127.         with tempReply do
  128.             if good then
  129.                 begin
  130.                     theGood := TRUE;
  131.                     theRefNum := vRefNum;
  132.                     theFileName := fName;
  133.                 end;
  134.     end;
  135.  
  136. {************************************}
  137.  
  138. {    NSSelectPutFile routines}
  139.  
  140.     function NSSelectPutDlg (item: INTEGER;
  141.                                     theDialog: DialogPtr): INTEGER;
  142.  
  143.         procedure SetPrompt;
  144.             var
  145.                 tempNum, tempV1, tempV2: INTEGER;
  146.                 tempHdl: Handle;
  147.                 tempRect: Rect;
  148.         begin
  149.             GetDItem(theDialog, kPutPrompt, tempNum, tempHdl, tempRect);
  150.             if tempHdl <> nil then
  151.                 begin
  152.                     if gNSSelect then
  153.                         SetIText(tempHdl, gNSAltPrompt)
  154.                     else
  155.                         SetIText(tempHdl, gNSPrompt);
  156.                 end;
  157.  
  158.             if gNSSelect then
  159.                 begin
  160.                     tempV1 := 0;
  161.                     tempV2 := 1;
  162.                 end
  163.             else
  164.                 begin
  165.                     tempV1 := 1;
  166.                     tempV2 := 0;
  167.                 end;
  168.  
  169.             GetDItem(theDialog, kPutNoSelectSwitch, tempNum, tempHdl, tempRect);
  170.             if tempHdl <> nil then
  171.                 SetCtlValue(ControlHandle(tempHdl), tempV1);
  172.             GetDItem(theDialog, kPutSelectSwitch, tempNum, tempHdl, tempRect);
  173.             if tempHdl <> nil then
  174.                 SetCtlValue(ControlHandle(tempHdl), tempV2);
  175.         end;
  176.  
  177.     begin
  178.         if gNSFirst then
  179.             begin
  180.                 SetPrompt;
  181.                 gNSFirst := FALSE;
  182.             end;
  183.  
  184.         if item = kPutSelectSwitch then
  185.             begin
  186.                 gNSSelect := TRUE;
  187.                 SetPrompt;
  188.                 item := 100;
  189.             end
  190.         else if item = kPutNoSelectSwitch then
  191.             begin
  192.                 gNSSelect := FALSE;
  193.                 SetPrompt;
  194.                 item := 100;
  195.             end;
  196.  
  197.         NSSelectPutDlg := item;
  198.     end;
  199.  
  200.     procedure NSSelectPutFile (thePrompt, theSelectPrompt, theOrigName: Str255;
  201.                                     var theGood: BOOLEAN;
  202.                                     var theRefNum: INTEGER;
  203.                                     var theFileName: Str255;
  204.                                     var theFlag: BOOLEAN);
  205.         var
  206.             tempPt: Point;
  207.             tempReply: SFReply;
  208.     begin
  209.         theGood := FALSE;
  210.  
  211.         tempPt.v := 40;
  212.         tempPt.h := 60;
  213.  
  214.         gNSFirst := TRUE;
  215.         gNSSelect := FALSE;
  216.         gNSPrompt := thePrompt;
  217.         gNSAltPrompt := theSelectPrompt;
  218.         SFPPutFile(tempPt, thePrompt, theOrigName, @NSSelectPutDlg, tempReply, kNSSelectPutID, nil);
  219.  
  220.         with tempReply do
  221.             if good then
  222.                 begin
  223.                     theGood := TRUE;
  224.                     theRefNum := vRefNum;
  225.                     theFileName := fName;
  226.                     theFlag := gNSSelect;
  227.                 end;
  228.     end;
  229.  
  230. {************************************}
  231.  
  232. {    NSIconPutFile routines}
  233.  
  234.     procedure DrawNSIcon (theNum: INTEGER;
  235.                                     theHiliteFlag: BOOLEAN);
  236.         var
  237.             tempBitMap: BitMap;
  238.             tempPort: GrafPtr;
  239.             tempRect, tempRect2: Rect;
  240.             tempFont, tempSize, tempLen, tempCenter: INTEGER;
  241.     begin
  242.         if (theNum > 0) and (theNum <= kMaxIcons) then
  243.             if gNSIcons[theNum] <> nil then
  244.                 begin
  245.                     GetPort(tempPort);
  246.  
  247.                     with gNSBoxs[theNum] do
  248.                         begin
  249.                             tempCenter := ((right + left) div 2);
  250.                             tempRect.top := top;
  251.                             tempRect.bottom := top + 32;
  252.                             tempRect.left := tempCenter - 16;
  253.                             tempRect.right := tempRect.left + 32;
  254.                         end;
  255.  
  256.                     PlotIcon(tempRect, gNSIcons[theNum]);
  257.  
  258.                     if gNSNameListPtr[theNum]^ <> '' then
  259.                         begin
  260.                             tempFont := tempPort^.txFont;
  261.                             tempSize := tempPort^.txSize;
  262.  
  263.                             TextFont(1);
  264.                             TextSize(10);
  265.  
  266.                             tempLen := StringWidth(gNSNameListPtr[theNum]^);
  267.                             with gNSBoxs[theNum] do
  268.                                 begin
  269.                                     tempRect2.top := bottom - 14;
  270.                                     tempRect2.bottom := bottom;
  271.                                     tempRect2.left := tempCenter - (tempLen div 2) - 4;
  272.                                     tempRect2.right := tempRect2.left + tempLen + 8;
  273.                                 end;
  274.  
  275.                             TextBox(POINTER(@gNSNameListPtr[theNum]^[1]), Length(gNSNameListPtr[theNum]^), tempRect2, teJustCenter);
  276.  
  277.                             TextFont(tempFont);
  278.                             TextSize(tempSize);
  279.                         end;
  280.  
  281.                     if theHiliteFlag then
  282.                         begin
  283.                             Hlock(gNSIcons[theNum]);
  284.                             tempBitMap.baseaddr := POINTER(ORD4(gNSIcons[theNum]^) + $80);
  285.                             tempBitMap.rowbytes := 4;
  286.                             SetRect(tempBitMap.bounds, 0, 0, 32, 32);
  287.  
  288.                             BitClr(Ptr($938), 0);
  289.                             CopyBits(tempBitMap, tempPort^.portBits, tempBitMap.bounds, tempRect, srcXor, nil);
  290.  
  291.                             HUnlock(gNSIcons[theNum]);
  292.  
  293.                             if gNSNameListPtr[theNum]^ <> '' then
  294.                                 begin
  295.                                     BitClr(Ptr($938), 0);
  296.                                     InvertRect(tempRect2);
  297.                                 end;
  298.                         end;
  299.                 end;
  300.     end;
  301.  
  302.     procedure NSIconItem (theWindow: WindowPtr;
  303.                                     itemNo: INTEGER);
  304.     begin
  305.         itemNo := itemNo - kPutIcon1 + 1;
  306.         if (itemNo >= 1) and (itemNo <= kMaxIcons) then
  307.             DrawNSIcon(itemNo, itemNo = gNSCurNum);
  308.     end;
  309.  
  310.     function NSIconPutDlg (item: INTEGER;
  311.                                     theDialog: DialogPtr): INTEGER;
  312.         var
  313.             tempNum, tempCount: INTEGER;
  314.             tempHdl: Handle;
  315.             tempRect: Rect;
  316.  
  317.         procedure SetPrompt;
  318.         begin
  319.             GetDItem(theDialog, kPutPrompt, tempNum, tempHdl, tempRect);
  320.             if tempHdl <> nil then
  321.                 SetIText(tempHdl, gNSPromptListPtr[gNSCurNum]^);
  322.         end;
  323.  
  324.     begin
  325.         if gNSFirst then
  326.             begin
  327.                 SetPrompt;
  328.  
  329.                 for tempCount := 1 to kMaxIcons do
  330.                     begin
  331.                         GetDItem(theDialog, tempCount - 1 + kPutIcon1, tempNum, tempHdl, gNSBoxs[tempCount]);
  332.                         tempHdl := @NSIconItem;
  333.                         SetDItem(theDialog, tempCount - 1 + kPutIcon1, tempNum, tempHdl, gNSBoxs[tempCount]);
  334.                     end;
  335.  
  336.                 gNSFirst := FALSE;
  337.             end;
  338.  
  339.         tempNum := item - kPutIcon1 + 1;
  340.         if (tempNum >= 1) and (tempNum <= kMaxIcons) then
  341.             begin
  342.                 item := 100;
  343.                 if (gNSIcons[tempNum] <> nil) and (tempNum <> gNSCurNum) then
  344.                     begin
  345.                         DrawNSIcon(gNSCurNum, FALSE);
  346.                         DrawNSIcon(tempNum, TRUE);
  347.                         gNSCurNum := tempNum;
  348.                         SetPrompt;
  349.                     end;
  350.             end;
  351.  
  352.         NSIconPutDlg := item;
  353.     end;
  354.  
  355.     procedure NSIconPutFile (theOrigName: Str255;
  356.                                     theDefaultNum: INTEGER;
  357.                                     theIcon1, theIcon2, theIcon3, theIcon4: Handle;
  358.                                     theName1, theName2, theName3, theName4: Str255;
  359.                                     thePrompt1, thePrompt2, thePrompt3, thePrompt4: Str255;
  360.                                     var theGood: BOOLEAN;
  361.                                     var theRefNum: INTEGER;
  362.                                     var theFileName: Str255;
  363.                                     var theNum: INTEGER);
  364.         var
  365.             tempPt: Point;
  366.             tempReply: SFReply;
  367.     begin
  368.         theGood := FALSE;
  369.  
  370.         if (theDefaultNum >= 1) and (theDefaultNum <= kMaxIcons) then
  371.             begin
  372.                 tempPt.v := 40;
  373.                 tempPt.h := 60;
  374.  
  375.                 gNSFirst := TRUE;
  376.                 gNSCurNum := theDefaultNum;
  377.                 gNSNameListPtr[1] := @theName1;
  378.                 gNSNameListPtr[2] := @theName2;
  379.                 gNSNameListPtr[3] := @theName3;
  380.                 gNSNameListPtr[4] := @theName4;
  381.                 gNSPromptListPtr[1] := @thePrompt1;
  382.                 gNSPromptListPtr[2] := @thePrompt2;
  383.                 gNSPromptListPtr[3] := @thePrompt3;
  384.                 gNSPromptListPtr[4] := @thePrompt4;
  385.                 gNSIcons[1] := theIcon1;
  386.                 gNSIcons[2] := theIcon2;
  387.                 gNSIcons[3] := theIcon3;
  388.                 gNSIcons[4] := theIcon4;
  389.  
  390.                 SFPPutFile(tempPt, '', theOrigName, @NSIconPutDlg, tempReply, kNSIconPutID, nil);
  391.  
  392.                 with tempReply do
  393.                     if good then
  394.                         begin
  395.                             theGood := TRUE;
  396.                             theRefNum := vRefNum;
  397.                             theFileName := fName;
  398.                             theNum := gNSCurNum;
  399.                         end;
  400.             end;
  401.     end;
  402.  
  403. {************************************}
  404.  
  405. {    NSGetFile routines}
  406.  
  407.     procedure DrawNSPopup;
  408.         var
  409.             tempStr: Str255;
  410.     begin
  411.         FrameRect(gNSBoxs[1]);
  412.         MoveTo(gNSBoxs[1].left, gNSBoxs[1].bottom);
  413.         LineTo(gNSBoxs[1].right, gNSBoxs[1].bottom);
  414.         LineTo(gNSBoxs[1].right, gNSBoxs[1].top);
  415.  
  416.         MoveTo(gNSBoxs[1].left + 4, gNSBoxs[1].bottom - 6);
  417.         if gNSCurNum = 0 then
  418.             DrawString('All Files')
  419.         else
  420.             begin
  421.                 GetItem(gNSMenuHdl, gNSCurNum + 2, tempStr);
  422.                 DrawString(tempStr);
  423.             end;
  424.     end;
  425.  
  426.     procedure NSPopUpItem (theWindow: WindowPtr;
  427.                                     itemNo: INTEGER);
  428.     begin
  429.         if itemNo = kGetPopUpItem then
  430.             DrawNSPopup;
  431.     end;
  432.  
  433.     function NSGetFileFilter (paramBlock: ParmBlkPtr): BOOLEAN;
  434.         var
  435.             tempFlag: BOOLEAN;
  436.             tempType: OSType;
  437.             tempCount: INTEGER;
  438.     begin
  439.         tempFlag := TRUE;
  440.  
  441.         tempType := paramBlock^.ioFlFndrInfo.fdType;
  442.         if gNSCurNum = 0 then
  443.             begin
  444.                 for tempCount := 0 to gNSMaxNum - 1 do
  445.                     if tempFlag then
  446.                         if gNSTypeListPtr^[tempCount] = tempType then
  447.                             tempFlag := FALSE;
  448.             end
  449.         else
  450.             begin
  451.                 if gNSTypeListPtr^[gNSCurNum - 1] = tempType then
  452.                     tempFlag := FALSE;
  453.             end;
  454.  
  455.         NSGetFileFilter := tempFlag;
  456.     end;
  457.  
  458.     function NSGetDlg (item: INTEGER;
  459.                                     theDialog: DialogPtr): INTEGER;
  460.         var
  461.             tempType: INTEGER;
  462.             tempHdl: Handle;
  463.             tempNum, tempNewNum: INTEGER;
  464.             tempLong: LongInt;
  465.             tempRect: Rect;
  466.             tempPt: Point;
  467.  
  468.     begin
  469.         if gNSFirst then
  470.             begin
  471.                 GetDItem(theDialog, kGetPopUpPrompt, tempNum, tempHdl, tempRect);
  472.                 if tempHdl <> nil then
  473.                     SetIText(tempHdl, gNSPrompt);
  474.  
  475.                 GetDItem(theDialog, kGetPopUpItem, tempType, tempHdl, gNSBoxs[1]);
  476.                 tempHdl := @NSPopUpItem;
  477.                 SetDItem(theDialog, kGetPopUpItem, tempType, tempHdl, gNSBoxs[1]);
  478.                 with gNSBoxs[1] do
  479.                     begin
  480.                         bottom := bottom - 1;
  481.                         right := right - 1;
  482.                     end;
  483.                 gNSFirst := FALSE;
  484.             end;
  485.  
  486.         if item = kGetPopUpItem then
  487.             begin
  488.                 if gNSCurNum = 0 then
  489.                     tempNum := 1
  490.                 else
  491.                     tempNum := gNSCurNum + 2;
  492.  
  493.                 CheckItem(gNSMenuHdl, tempNum, TRUE);
  494.                 InsertMenu(gNSMenuHdl, -1);
  495.                 tempPt := gNSBoxs[1].topleft;
  496.                 LocalToGlobal(tempPt);
  497.                 BitClr(Ptr($938), 0);
  498.                 InvertRect(gNSBoxs[1]);
  499.                 tempLong := PopUpMenuSelect(gNSMenuHdl, tempPt.v, tempPt.h, tempNum);
  500.                 BitClr(Ptr($938), 0);
  501.                 InvertRect(gNSBoxs[1]);
  502.                 DeleteMenu(kGetPopUpID);
  503.                 CheckItem(gNSMenuHdl, tempNum, FALSE);
  504.  
  505.                 if tempLong <> 0 then
  506.                     begin
  507.                         tempNewNum := LoWord(tempLong);
  508.                         if tempNewNum <> tempNum then
  509.                             begin
  510.                                 if tempNewNum = 1 then
  511.                                     gNSCurNum := 0
  512.                                 else
  513.                                     gNSCurNum := tempNewNum - 2;
  514.  
  515.                                 item := 101;
  516.                                 EraseRect(gNSBoxs[1]);
  517.                                 DrawNSPopup;
  518.                             end;
  519.                     end;
  520.             end;
  521.  
  522.         NSGetDlg := item;
  523.     end;
  524.  
  525.     procedure NSGetFile (thePrompt: Str255;
  526.                                     theNumTypes: INTEGER;
  527.                                     theTypeListPtr: SFTypeListPtr;
  528.                                     theNameListPtr: StrArrayPtr;
  529.                                     var theGood: BOOLEAN;
  530.                                     var theRefNum: INTEGER;
  531.                                     var theFileName: Str255;
  532.                                     var theType: OSType);
  533.         var
  534.             tempPt: Point;
  535.             tempReply: SFReply;
  536.             tempCount: INTEGER;
  537.     begin
  538.         theGood := FALSE;
  539.         theRefNum := -1;
  540.         theFileName := '';
  541.         theType := '    ';
  542.  
  543.         if (theNumTypes > 0) and (theTypeListPtr <> nil) then
  544.             begin
  545.                 gNSCurNum := 0;
  546.                 gNSMaxNum := theNumTypes;
  547.                 gNSTypeListPtr := theTypeListPtr;
  548.                 gNSPrompt := thePrompt;
  549.                 gNSFirst := TRUE;
  550.                 gNSMenuHdl := NewMenu(kGetPopUpID, 'NS');
  551.                 AppendMenu(gNSMenuHdl, 'All Files;(-');
  552.                 for tempCount := 1 to theNumTypes do
  553.                     AppendMenu(gNSMenuHdl, theNameListPtr^[tempCount]);
  554.  
  555.                 tempPt.v := 40;
  556.                 tempPt.h := 60;
  557.                 SFPGetFile(tempPt, '', @NSGetFileFilter, theNumTypes, theTypeListPtr^, @NSGetDlg, tempReply, kNSGetID, nil);
  558.  
  559.                 DisposeMenu(gNSMenuHdl);
  560.  
  561.                 with tempReply do
  562.                     if good then
  563.                         begin
  564.                             theGood := TRUE;
  565.                             theRefNum := vRefNum;
  566.                             theFileName := fName;
  567.                             theType := fType;
  568.                         end
  569.             end;
  570.     end;
  571.  
  572. end.